package com.wf.controller;
import com.wf.bean.UserWf;

import com.wf.service.UserWfMapperService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


//@CrossOrigin
@RestController
public class UserWfController {

    @RequestMapping("/hello")
    public String handle01(){
        return "你好, Spring Boot 2!";
    }

    // getwfbyid
    @Autowired
    UserWfMapperService userWfMapperService;


    @RequestMapping("/getwfbyid")
    public UserWf getwf(@RequestParam("id") Integer id){
        UserWf userwf = userWfMapperService.getUserWfById(id);
        return userwf;
    }
    // 获取Wf的总个数
    @RequestMapping("/getWfCount")
    public int getWfCount(){
        return userWfMapperService.getWfCount();
    }

    // getallwf
    @RequestMapping("/getallwf")
    public ArrayList<UserWf> getallwf(){
        ArrayList<UserWf> userAll = userWfMapperService.getUserAll();
        for (UserWf u:userAll) {
            System.out.println(u);
        }
        return userAll;
    }

    // insertOneWf
    @PostMapping("/insertOneWf") // 用前端代码测试
    public Map insertOneWf(@RequestBody UserWf userWf){
        System.out.println(userWf);
        Map<String,Object> map = new HashMap<>();
//        map.put("content",content);
        userWfMapperService.insertOneWf(userWf);
        map.put("success","添加成功");
        return map;
    }

    //deleteOneWfId
    @PostMapping("/deleteOneWfId") // 用前端代码测试
    public Map deleteOneWfId(@RequestBody UserWf userWf){
        System.out.println(userWf);
        System.out.println(userWf.getId());
        // 应该查一下有没有这个id的wf
        Map<String,Object> map = new HashMap<>();
        userWfMapperService.deleteOneWfId(userWf.getId());
        map.put("success","删除成功");
        return map;
    }


    //updateOneWf
    @PostMapping("/updateOneWf") // 用前端代码测试
    public Map updateOneWf(@RequestBody UserWf userWf){
        System.out.println(userWf);
        System.out.println(userWf.getClass().getName());
        Map<String,Object> map = new HashMap<>();
        // 应该查一下有没有这个id的wf
        userWfMapperService.updateOneWf(userWf);
        map.put("success","修改成功");
        return map;
    }



    // 批量查询 √
    @RequestMapping(value = "/getItByIds")
    public ArrayList<UserWf> getItByIds(@RequestParam("ids") List<Integer> ids) { //前端传数组会报错,传字符串吧 Required request parameter 'ids' for method parameter type List is not present
        System.out.println("getItByIds");
        System.out.println("ids:"+ids);
       return userWfMapperService.getItByIds(ids);
    }



    // 批量删除 √
    @PostMapping(value = "/deleteMulWfByIds")
    public void deleteMulWfByIds(@RequestBody Map<String,List<Integer>> ids) {
        System.out.println("deleteMulWfByIds"+ids);
        userWfMapperService.deleteMulWfByIds(ids.get("ids"));
    }

    // 批量添加 方式一(主流 键值对的形式) √
    @PostMapping(value = "/insertMulWf")
    public void insertMulWf(@RequestBody Map<String,List<UserWf>> userWfs) {
        System.out.println("批量添加1");
        System.out.println(userWfs);
        userWfMapperService.insertMulWf(userWfs.get("userWfList"));
    }

    // 批量添加 方式二( 只传一个数组) √
   /* @PostMapping(value = "/insertMulWf2")
    public void insertMulWf(@RequestBody List<UserWf> userWfs) {
        System.out.println("批量添加2");
        System.out.println(userWfs);
        userWfMapperService.insertMulWf(userWfs);
    }*/

    // 批量修改  (有问题)
    @PostMapping(value = "/updateMulWf")
    public void updateMulWf(@RequestBody Map<String,List<UserWf>> userWfs) {
        System.out.println("批量修改");
        System.out.println(userWfs);
        userWfMapperService.updateMulWf(userWfs.get("userWfList"));
    }


    // 自定义分页简单查询
    @RequestMapping(value = "/getPageList")
    public Map<String,Object> getPageList(@RequestParam("pageNum") int pageNum,@RequestParam("pageSize") int pageSize,@RequestParam(value = "wfname",required = false) String wfname){

        Map<String,Object> map = new HashMap<>();
        int count = userWfMapperService.getWfCount();
        ArrayList<UserWf> userList = userWfMapperService.getPageList(pageNum, pageSize);
        if ( count>=0 && userList.size()>0){
            map.put("total", count);
            map.put("dataList", userList);
            map.put("current", pageNum);
            map.put("size", pageSize);
            map.put("message", "成功");
            map.put("returnCode", "1000");
        }else{
            map.put("info", "error 未查询到结果");
            return  map;
        }


        return map;
    }

    // 简单条件模糊查询个数
    @RequestMapping(value = "/valueGetWfCountByName")
    public  int vaguePageSearch(@RequestParam("wfname") String wfname) { //前端传数组会报错,传字符串吧 Required request parameter 'ids' for method parameter type List is not present
        System.out.println("wfname:"+wfname);

        return userWfMapperService.valueGetWfCountByName(wfname);

    }

    // 模糊查询
    @RequestMapping(value = "/vagueSearch")
    public ArrayList<UserWf> vagueSearch(@RequestParam("wfname") String wfname) { //前端传数组会报错,传字符串吧 Required request parameter 'ids' for method parameter type List is not present
        System.out.println("wfname:"+wfname);
        return userWfMapperService.vagueSearch(wfname);

    }

    // 模糊分页查询
    @RequestMapping(value = "/vaguePageSearch")
    public Map<String,Object> vaguePageSearch(@RequestParam("pageNum") int pageNum,@RequestParam("pageSize") int pageSize,@RequestParam("wfname") String wfname) { //前端传数组会报错,传字符串吧 Required request parameter 'ids' for method parameter type List is not present
        System.out.println("wfname:"+wfname);
        int count = userWfMapperService.valueGetWfCountByName(wfname);
        Map<String,Object> map = new HashMap<>();
        ArrayList<UserWf> userList = userWfMapperService.vaguePageSearch(pageNum, pageSize,wfname);
        if ( count>=0 && userList.size()>0){
            map.put("total", count);
            map.put("dataList", userList);
            map.put("current", pageNum);
            map.put("size", pageSize);
            map.put("message", "成功");
            map.put("returnCode", "1000");
        }else{
            map.put("info", "error 未查询到结果");
            return  map;
        }

        return map;
//        return userWfMapperService.vagueSearch(wfname);

    }


}